<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选项卡（组件）</title>
    <style>
        ul,
        li {
            list-style: none;
        }


        .box {
            width: 300px;
            height: 200px;
            color: black;
            font-size: larger;
            text-align: center;
            line-height: 200px;
            background-color: darkkhaki;
            display: none;
        }

        .active {
            display: block;
        }
    </style>
</head>

<body>
    <div id="app">
        <titlechild @childevent="fatherEvent"></titlechild>
        <ul>
            <li v-for="(item,index) in contents">
                <div :class="{box:isActive,active:(index == flag)}">{{ item.item }}</div>
            </li>
        </ul>
    </div>

    <script src="../vue.js"></script>

    <script>
        let titlechild = {
            props: ["contents"],
            template: `
            <ul>
                <li v-for="(item,index) in title" @click="changeIndex(index)">
                <button>{{ item.item }}</button>
                </li>
            </ul>
            `,
            data() {
                return {
                    title: [
                        { id: 1, item: 'html' },
                        { id: 2, item: 'css' },
                        { id: 3, item: 'js' },
                        { id: 4, item: 'vuejs' }
                    ]
                }
            },
            methods: {
                changeIndex(index) {
                    this.$emit("childevent", index)
                }
            }
        };

        let vw = new Vue({
            el: "#app",
            data: {
                isActive: true,
                flag: 0,
                contents: [
                    { item: 'html-contents' },
                    { item: 'css-contents' },
                    { item: 'js-contents' },
                    { item: 'vuejs-contents' }]
            },
            components: {
                titlechild
            },
            methods: {
                fatherEvent(index) {
                    this.flag = index;
                },
            }
        })
    </script>
</body>

</html>